﻿using AutoMapper;
using IMES.Common.Domain.KeepModel.Process;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Keep.API.Appliction.Command.ProcessFile
{
    /// <summary>
    /// 创建工步
    /// </summary>
    public class WorkingProcedureCreateHandler : IRequestHandler<WorkingProcedureCreateCommon, Result>
    {
        private readonly IBaseRepositorys<WorkingProcedure> _workingProcedureRepository;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="workingProcedureRepository"></param>
        /// <param name="mapper"></param>
        public WorkingProcedureCreateHandler(IBaseRepositorys<WorkingProcedure> workingProcedureRepository, IMapper mapper)
        {
            _workingProcedureRepository = workingProcedureRepository; 
        }

        public Task<Result> Handle(WorkingProcedureCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success, Messege = "添加成功" };
            try
            {
                 
                var list = _workingProcedureRepository.AddRange(request.WorkingProcedure);
                if(list == null )
                {
                    throw new Exception("添加失败"); 
                }
               

                return Task.FromResult(result);

            }
            catch (Exception ex)
            {
                result.Code = (int)CodeEnum.error;
                result.Messege = "添加失败";
            }
            return Task.FromResult(result);
        }
       
    }
}
